package com.eagle.epay.interceptor;

import com.eagle.epay.constant.EPayConstant;
import com.eagle.epay.constant.ResponseEnum;
import com.eagle.epay.entity.User;
import com.eagle.epay.exception.ServiceException;
import com.eagle.epay.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {

	public static final String skip_pattern = "^.*/api-docs|/swagger-ui.*|/swagger.*|.*\\.png|.*\\.css|.*\\.js|.*\\.html|/favicon.ico|.*/swaggerAPI/.*$";

	/**
	 * false 中断流程
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		log.info("Enter preHandle...");
		User user = (User) request.getSession().getAttribute(EPayConstant.CURUSER);
		Pattern skipPattern = Pattern.compile(skip_pattern);
		String uri = request.getRequestURI();
//		if(null == user && !skipPattern.matcher(uri).matches()){
//			log.info("user is null when access the URL {}", uri);
//			throw new ServiceException("user is null");
//		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

	}
}
